package nju.mscs.server.businessLogic;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import nju.mscs.data.*;
import nju.mscs.data.object.Database;
import nju.mscs.data.object.GiftList;
import nju.mscs.data.object.Goods;
import nju.mscs.data.object.Inventory;
import nju.mscs.data.object.PromotionList;
import nju.mscs.data.object.SaleRecord;
import nju.mscs.data.object.User;
import nju.mscs.data.object.UserList;
import nju.mscs.data.object.VipList;
import nju.mscs.data.object.VipUpdataList;

public class Controller {
	private static int FIRST=0;
	private static int NotFirst=1;
	private final static String sumDBName="sumDB.ser";
	private final static String fileName="Database.ser";
	private final static int size=Integer.parseInt(Configration.getIniKey("size"));//大小;
	private int currentSize;
	private DataServerManager dsm;
	private NetServerManager nsm;
	public static Database sumDB;
	private Calendar ca;
	private ArrayList<Database> dbl=new ArrayList<Database>();
	public Controller(){
		dsm=new DataServerManager();
		try {
			sumDB=dsm.readData(this.sumDBName);
			System.out.println(size);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			sumDB = DataServerManager.initialize();
			System.out.println("AAAA");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<size;i++){
			try {
				dbl.add(dsm.readData(i+this.fileName));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		nsm=new NetServerManager(dbl);

	}
	public void start(){
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					nsm.startDownloadServer(Integer.parseInt(Configration.getIniKey("serverportdown")));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}}).start();
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					nsm.startUploadServer(Integer.parseInt(Configration.getIniKey("serverportup")));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}}).start();
	}
	public boolean close(){
		for(Database db:dbl){
			db.replace(sumDB);
			for(String[] infor:SM.add)
				db.addGoods(infor[1], Double.parseDouble(infor[2]), infor[0], 0);
			for(String infor:SM.delete)
				db.deleteGoods(infor);
			for(String[] infor:SM.change){
				Goods good=db.searchGoodsById(infor[0]);
				good.setName(infor[1]);
				good.setPrice(Double.parseDouble(infor[2]));
			}
		}
		try {
			dsm.savaDate(this.sumDBName, sumDB);
			System.out.println(dbl.size());
			for(Database db:dbl){
				dsm.savaDate(db.getShopId()+this.fileName, db);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public int getCurrentSize(){
		return dbl.size();
	}
}
